import { defineStore } from 'pinia'
import { ref } from 'vue'



const userUserStore = defineStore("user",() => {
    const user = ref({
        userId: '',
        username: '',
        avatar: '',
        enemyUserId: '',
        enemyUserAvatar: '',
        enemyUserUsername: '',
        socket: null,
        currentMap: [],
        currentUser: '',
        currentColor: '',
        userColor: '',
        status: '',
    })

    const setUser = (data) => {
        user.value.userId = data.userId
        user.value.username = data.username
        user.value.avatar = data.avatar
    }

    const setEnemy = (data) => {
        user.value.enemyUserId = data.userId
        user.value.enemyUserAvatar = data.username
        user.value.enemyUserUsername = data.avatar
    }

    const clearUser = () => {
        user.value.userId = ''
        user.value.username = ''
        user.value.avatar = ''
    }

    const clearEnemy = () => {
        user.value.enemyUserId = ''
        user.value.enemyUserAvatar = ''
        user.value.enemyUserUsername = ''
    }

    const clearChess = () => {
        user.value.socket = null
        user.value.currentMap = []
        user.value.currentUser = ''
        user.value.status = ''
        user.value.currentColor = ''
        user.value.userColor = ''
    }

    const clearMessage = () => {
        clearChess()
        clearEnemy()
        clearUser()
    }


    return {
        user,
        setUser,
        setEnemy,
        clearUser,
        clearChess,
        clearEnemy,
        clearMessage
    }
},{
    // 开启数据持久化
    persist: true
    // ...省略
});